import hashlib
import socket
import os
try:
    import clipboard
except:
    os.system("pip install clipboard")
    import clipboard

# 保证在传输结束后，端口可复用
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 取得本地IP地址
conn_trycount = 0
while conn_trycount<3:
    try:
        conn_trycount += 1
        conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        conn.connect(("223.5.5.5",53))
        ip = conn.getsockname()[0]
        print(' > Here ', ip)
        conn.close()

        ip_pre3 = ip.split('.')
        ip_4 = ip_pre3[3]
        ip_pre3[3] = ''
        ip_pre3 = '.'.join(ip_pre3)
        break
    except:
        conn_trycount += 1


# 支持(192.168.x.)x的两种输入格式
server = input(' > Get from (%s) ' %ip_pre3)
port = 1079
server = server.split('.')
if len(server) == 1:
    server = "%s%s" %(ip_pre3,server[0])
elif  len(server) == 4:
    server = '.'.join(server)

s.connect((server, port))

get = s.recv(131072)
get_all = get
while get:
    get = s.recv(131072)
    get_all += get

get_all = get_all.split(b'0x00')[1].decode("utf-8")
print(get_all)
print(" > Get the contend ",end='')
try:
    clipboard.copy(get_all)
    print("to clipboard")
except:
    print('\n')
# input()
